package com.yikome.config;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.itmuch.lightsecurity.exception.LightSecurityException;

import lombok.extern.java.Log;

@Log
@RestControllerAdvice
public class GlobalExceptionHandler {
	
	@ExceptionHandler(value = IllegalArgumentException.class)
    public ApiResult IllegalArgumentExceptionHandler(HttpServletRequest request, Exception exception) {
        log.info(exception.getMessage());
        return new ApiResult(ApiResult.CODE_ERROR_COMMON, exception.getMessage());
    }

	
	@ExceptionHandler(value = Exception.class)
    public ApiResult exceptionHandler(HttpServletRequest request, Exception exception) {
        log.info(exception.getMessage());
        return new ApiResult(ApiResult.CODE_ERROR_COMMON, exception.getMessage());
    }
	
	@ExceptionHandler(value = LightSecurityException.class)
    public ApiResult iightSecurityExceptionHandler(HttpServletRequest request, Exception exception) {
        String errorMsg = exception.getMessage() ;
        log.info(errorMsg);
        if(StringUtils.equalsIgnoreCase("Token invalided.", errorMsg))return ApiResult.builder().code(ApiResult.CODE_ERROR_TOKEN_INVALIDED).message("登陆失效..").build() ; //-1
        
        if(StringUtils.equalsIgnoreCase("no access", errorMsg))return ApiResult.builder().code(ApiResult.CODE_ERROR_NO_ACCESS).message("权限不足..").build() ; //-2
        
        if(StringUtils.equalsIgnoreCase("无权...", errorMsg))return ApiResult.builder().code(ApiResult.CODE_ERROR_NO_ACCESS).message("权限不足..").build() ; //-2
        
        return ApiResult.builder().code(ApiResult.CODE_ERROR_TOKEN_MISS).message("登陆信息缺失...").build(); //-3
    }
}
